<?php namespace Common\Model;
use Hdphp\Model\Model;
class Address extends Model{
	protected $table = 'address';
//	注册信息的自动验证
	protected $validate = array(
		array('province','required','省名不能为空',3,3),
		array('city','required','市名不能为空',3,3),
		array('district','required','区名不能为空',3,3),
		array('detail','required','详细地址不能为空',3,3),
		array('consignee','required','收货人姓名不能为空',3,3),
		array('tel','required','收货人手机不能为空',3,3),
		array('tel','phone','手机号格式不对',3,3),
	);
	
//	2.新增地址
	public function store(){
		if(!$this->create()) return false;
		$this->data['user_uid'] = $_SESSION['uid'];
//		如果有设置默认地址，则将原来的默认地址的状态改为0
		if(isset($this->data['index'])){
			$this->clearIndex($this->data['user_uid']);
		}
//		保存地址
		return $this->add();
	}
//	3.删除地址
	public function addrDel(){
		$adid = Q('get.adid',0,'intval');
		$this->where("adid = $adid")->delete();
		return TRUE;
	}
	
//	4.修改地址
	public function edit(){
		if(!$this->create()) return FALSE;
//		p($this->data);die;
		if(isset($this->data['index'])){
			$this->clearIndex($_SESSION['uid']);
		}
//		更新数据表
		$this->save();
		return true;
	}
	
	
//	清除默认地址
	public function clearIndex($id){
		$this->where("user_uid = $id")->save(array('index'=>0));
	}
	
	
	
	
	
	
	
	
	
	
	
}
 ?>